package fun._6742.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun._6742.exception.CustomException;
import fun._6742.mapper.ProductOrderMapper;
import fun._6742.model.dataobject.ProductOrderDO;
import fun._6742.model.valueobject.OrderVO;
import fun._6742.service.AdminOrderService;
import fun._6742.service.ProductOrderItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AdminOrderServiceImpl extends ServiceImpl<ProductOrderMapper, ProductOrderDO> implements AdminOrderService {

    @Resource
    ProductOrderItemService productOrderItemService;

    @Override
    public Map<String, Object> findOne(long id, int status) {
        ProductOrderDO orderDO = null;
        if (status == -1) {
            orderDO = getById(id);
        } else {
            orderDO = getOne(new QueryWrapper<ProductOrderDO>().eq("id", id).eq("del", status));
        }
        if (orderDO == null) {
            log.error("findOrder:res:false,id:{}", id);
            throw new CustomException(HttpStatus.NOT_FOUND, "order(id=" + id + " does not exist");
        }
        OrderVO orderVO = new OrderVO();
        BeanUtil.copyProperties(orderDO, orderVO);
        orderVO.setStatus(orderDO.getDel());
        Map<String, Object> result = new HashMap<>();
        result.put("order", orderVO);
        result.put("items", productOrderItemService.listAllItems(id));
        return result;
    }

    @Override
    public Map<String, Object> listAll(int page, int size, int status) {
        IPage<ProductOrderDO> orderDOIPage = null;
        if (status == -1) {
            orderDOIPage = page(new Page<>(page, size));
        } else {
            orderDOIPage = page(new Page<>(page, size), new QueryWrapper<ProductOrderDO>().eq("del", status));
        }
        if (orderDOIPage == null) {
            throw new CustomException(HttpStatus.NOT_FOUND, "no records");
        }
        Map<String, Object> map = new HashMap<>(3);
        map.put("total_page", orderDOIPage.getPages());
        map.put("total_record", orderDOIPage.getTotal());
        map.put("data", orderDOIPage.getRecords().stream().map(obj -> {
            OrderVO orderVO = new OrderVO();
            BeanUtil.copyProperties(obj, orderVO);
            orderVO.setStatus(obj.getDel());
            Map<String, Object> result = new HashMap<>();
            result.put("order", orderVO);
            result.put("items", productOrderItemService.listAllItems(orderVO.getId()));
            return result;
        }).collect(Collectors.toList()));
        return map;
    }

    @Override
    public void changeStatus(long id, int status) {
        ProductOrderDO productOrderDO = getById(id);
        productOrderDO.setDel(status);
        updateById(productOrderDO);
    }
}
